﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Xml.Serialization;

namespace testDSML
{
  class testXmlSerialize
  {

    public static void go()
    {

      BatchRequest batch = new BatchRequest();
      batch.ID = "1234";
      batch.Requests.Add(new AuthRequest() { Item = "test1", Type = reqTypeEnum.One });
      batch.Requests.Add(new AuthRequest() { Item = "test2", Type = reqTypeEnum.One });

      XmlDocument doc = new XmlDocument();
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);
      XmlSerializer xs = new XmlSerializer(typeof(BatchRequest));
      xs.Serialize(sw, batch);
      Console.WriteLine(sb.ToString());
    }

  }


  [Serializable()]
  [XmlRoot(ElementName="batchRequest", Namespace = "", IsNullable = false)]
  [XmlInclude(typeof(Request))]
  [XmlInclude(typeof(AuthRequest))]
  public class BatchRequest
  {



    public BatchRequest()
    {
      Requests = new System.Collections.ObjectModel.ObservableCollection<Request>();
    }




    [System.Xml.Serialization.XmlAttribute()]
    public string ID { get; set; }




    [XmlElement(ElementName="request")]
    //[XmlArrayItem(Type = typeof(Request)),
    //XmlArrayItem(Type = typeof(AuthRequest))]
    public System.Collections.ObjectModel.ObservableCollection<Request> Requests { get; set; }
    
  }





  public enum reqTypeEnum
  {
    One,
    Two,
    Three,
    Four,
  }





  [Serializable()]
  [XmlInclude(typeof(User))]
  public abstract class Request
  {

    public Request()
    {
      Users = new System.Collections.ObjectModel.ObservableCollection<User>();
    }

    [XmlAttributeAttribute(AttributeName = "item")]
    public string Item { get; set; }


    [XmlArrayItem(Type = typeof(User))]
    public System.Collections.ObjectModel.ObservableCollection<User> Users { get; set; }
  
  }





  [XmlType(TypeName = "authRequest")]
  public class AuthRequest : Request
  {
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public reqTypeEnum Type;

  }





  [XmlType(TypeName = "user")]
  public class User
  { 
  
  }



}
